package mmc.ui;

import static mmc.resource.Text12p.glText12p;
import static org.lwjgl.opengl.GL11.*;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Map;

import mmc.action.Action;
import mmc.action.BartenderKey;
import mmc.action.skill.Skill;
import mmc.resource.Sprite2D;
import mmc.resource.config.SavedVariables;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

public class Bartender extends Component {
	public static Bartender bb;
	public KeyBinding binding;
	public ArrayList<BartenderKey> keys = new ArrayList<BartenderKey>();
	
	public Bartender(float pos_x, float pos_y, float size_x, float size_y) {
		super(pos_x, pos_y, size_x, size_y);
		bb = this;
		init_keybindings();
	}
	
	public void init_keybindings()
	{
		binding = new KeyBinding();
		
		Map<Integer, String> map = SavedVariables.loadSettings("bartender.txt");
		
		
		
		for(int i=0;i<9;i++){			
			BartenderKey bart = new BartenderKey("Key_"+(i+1), null, Keyboard.getKeyIndex(""+(i+1))); 
			KeyBinding.addBartenderShortcut(keys, bart);
			
			//keys.add(bart);
			//KeyBinding.binding.put(keys.get(i).defaultKey, keys.get(i));					
		}
		
		if(map != null){
			for(Map.Entry<Integer, String> entry : map.entrySet()){
				BartenderKey b = (BartenderKey)KeyBinding.binding.get(entry.getKey());
				try {
					if(b != null)
						b.action = (Action) Class.forName(entry.getValue()).getConstructor().newInstance();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
				
		
		//SavedVariables.saveSettings(keys, "bartender.txt");
	}

	@Override
	public void render() {
	
		glColor4f(1,1,1,1);
		GL11.glBegin(GL11.GL_LINES);
		GL11.glVertex2f(x, y);
		GL11.glVertex2f(x+width, y);
		GL11.glEnd();
				
		float distance = width / keys.size();
		
		for(int i = 0; i <= keys.size(); i++)
		{
			glColor4f(1,1,1,1);
			GL11.glBegin(GL11.GL_LINES);
			GL11.glVertex2f(x+distance*i, y);
			GL11.glVertex2f(x+distance*i, y+height);
			GL11.glEnd();

			if(i < keys.size())
			{
				if(keys.get(i).action != null){
					Action act = keys.get(i).action; //KeyBinding.binding.get(i+2);
					Sprite2D skill = act.texture;
					if (skill != null){										
						skill.draw2f(x+distance*i+0.001f, y, distance, height);
						
						if(Skill.globalCD)
						{
							glColor4f(1,0.3f,0.2f, 0.5f);
							float help = 1-((mmc.Constants.GLOBAL_CD_TIME - Skill.globalTimer) / mmc.Constants.GLOBAL_CD_TIME);
							
							
							GL11.glBegin(GL11.GL_QUADS);
							GL11.glVertex2f(x+distance*i, y);
							GL11.glVertex2f(x+distance*i, y+height);
							GL11.glVertex2f(x+distance*i+distance*help, y+height);
							GL11.glVertex2f(x+distance*i+distance*help, y);
							GL11.glEnd();
						}
						
						if(Skill.cooldowns.containsKey(act)){
							glColor4f(1,0,0, 0.5f);
							float help = 1-(((Skill)act).cooldown - Skill.cooldowns.get(act)) / ((Skill)act).cooldown;
							
							//System.out.println("CDCDCD " + ((Skill)act).enable);
							
							GL11.glBegin(GL11.GL_QUADS);
							GL11.glVertex2f(x+distance*i, y);
							GL11.glVertex2f(x+distance*i, y+height);
							GL11.glVertex2f(x+distance*i+distance*help, y+height);
							GL11.glVertex2f(x+distance*i+distance*help, y);
							GL11.glEnd();
						}
					}
				}
				
				glText12p(Keyboard.getKeyName(keys.get(i).defaultKey) +"", x+distance*i+0.009f, y+height*0.1f);
			}
		}
	}

	@Override
	public void safeChangesOnShutdown(){
		SavedVariables.saveSettings(keys, "bartender.txt");
	}
}
